﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace VariablesInfo
{
    /// <summary>
    /// Implements the main UI for the plug-in
    /// </summary>
    public partial class MainForm : Form
    {
		#region Fields (2) 

        /// <summary>
        /// Stores cursor values for the form.
        /// </summary>
        private Stack<Cursor> myCursor = new Stack<Cursor>();
        /// <summary>
        /// Backing field for the <see cref="VariableInfoData"/> property.
        /// </summary>
        private List<string[]> variableInfoData = new List<string[]>();

		#endregion Fields 

		#region Constructors (2) 

        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        /// <param name="varInfoData">List of string arrays that contain variables information</param>
        public MainForm(List<string[]> varInfoData)
        {
            this.variableInfoData = varInfoData;  //Set the value of the field that stores the variable information
            InitializeComponent();  //Initialize the components for the form
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();  //Initialize the components for the form
        }

		#endregion Constructors 

		#region Methods (6) 

		// Private Methods (6) 

        /// <summary>
        /// Handles the opening event for the <see cref="contextDataGrid"/> control.
        /// </summary>
        /// <param name="sender">Object that initiated the event</param>
        /// <param name="e">Arguments for the event</param>
        /// <remarks>This method enables or disables items on the context menu based on the select state of cells in the grid.</remarks>
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (gridVariableInfo.SelectedCells.Count > 0)  //Check to see if any cells in the grid are selected
            {
                menuCopy.Enabled = true;  //Enable Copy if any cells are selected
            }
            else
            {
                menuCopy.Enabled = false;  //Disable Copy if no cells are selected
            }
            if (gridVariableInfo.AreAllCellsSelected(true))  //Check to see if all the cells in the grid are selected
            {
                menuSelectAll.Enabled = false;  //Disable Select All
                gridVariableInfo.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;  //Include the column headers in the clipboard data
            }
            else
            {
                menuSelectAll.Enabled = true;  //Enable Select All
                gridVariableInfo.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithAutoHeaderText;  //Let the control determine whether headers are included in the clipboard data
            }
        }

        /// <summary>
        /// Loads data into the <see cref="gridVariableInfo"/> control
        /// </summary>
        private void LoadGrid()
        {
            myCursor.Push(this.Cursor);  //Save the current cursor
            this.Cursor = Cursors.WaitCursor;  //Show the wait cursor

            foreach (string[] varInfo in variableInfoData)  //Loop through the list of string arrays
            {
                gridVariableInfo.Rows.Add(varInfo);  //Add a row to the grid
            }

            this.Cursor = myCursor.Pop();  //Return the cursor to its previous value
        }

        /// <summary>
        /// Handles the load event for the <see cref="MainForm"/> form.
        /// </summary>
        /// <param name="sender">Object that initiated the event</param>
        /// <param name="e">Arguments for the event</param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = Properties.Resources.MainFormTitleText;  //Set the title text for the form
        }

        /// <summary>
        /// Handles the shown event for the <see cref="MainForm"/> form.
        /// </summary>
        /// <param name="sender">Object that initiated the event</param>
        /// <param name="e">Arguments for the event</param>
        private void MainForm_Shown(object sender, EventArgs e)
        {
            LoadGrid();  //Load the grid
        }

        /// <summary>
        /// Handles the click event for the <see cref="menuCopy"/> control.
        /// </summary>
        /// <param name="sender">Object that initiated the event</param>
        /// <param name="e">Arguments for the event</param>
        private void menuCopy_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetDataObject(gridVariableInfo.GetClipboardContent());  //Put the clipboard content from the grid into the clipboard
            }
            catch (System.Runtime.InteropServices.ExternalException ex)  //Catch an exception, if it occurs
            {
                MessageBox.Show(ex.Message, Properties.Resources.MainFormClipboardErrorText, MessageBoxButtons.OK, MessageBoxIcon.Error);  //Display a message stating that an error occurred
            }
        }

        /// <summary>
        /// Handles the click event for the <see cref="menuSelectAll"/> control.
        /// </summary>
        /// <param name="sender">Object that initiated the event</param>
        /// <param name="e">Arguments for the event</param>
        private void menuSelectAll_Click(object sender, EventArgs e)
        {
            gridVariableInfo.SelectAll();  //Select all cells in the grid
        }

		#endregion Methods 

        /// <summary>
        /// Gets or sets a list of information about the variables in the library
        /// </summary>
        public List<string[]> VariableInfoData
        {
            get { return variableInfoData; }  //Return the value of the backing field for the property
            set { variableInfoData = value; }  //Set the value of the backing field for the property
        }
    }
}